layui.use(['form', 'layer', 'jquery', 'laydate'],
    function () {
        $ = layui.jquery;
        var form = layui.form,
            layer = layui.layer;
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#birthday' //指定元素
            , trigger: 'click'
        });

        //自定义验证规则
        form.verify({
            nikename: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            },
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(add)',
            function (data) {
                //定义存储复选框勾选的爱好内容
                let hobby = [];
                var attr = $('input[type="checkbox"][name="hobby"]:checked').each(function () {
                    hobby.push($(this).attr("title"));
                })
                data.field.hobby = hobby.toString();
                data.field.enable = data.field.enable=='启用'?1:0;
                // console.log(data.field);


                $.ajax({
                    url: 'http://localhost:8080/back/user/insertUser',
                    data: JSON.stringify(data.field),
                    contentType:'application/json',
                    type: 'post',
                    dataType:'json',
                    headers:{'username':localStorage.getItem("loginUser")},
                    success: function (res) {
                        console.log("后端返回de数据", res)
                        if (res.msg == "success") {
                            //发异步，把数据提交给php
                            layer.alert("增加成功", {
                                    icon: 6
                                },
                                function () {
                                    //关闭当前frame
                                    xadmin.close();

                                    // 可以对父窗口进行刷新
                                    xadmin.father_reload();
                                });
                        } else {
                            layer.alert("增加失败");
                        }
                    }
                })
                return false;
            });

    });